package ru.vsu.cs.pm;

import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;

import ru.vsu.cs.pm.pages.LoginPage;
/**
 * User: Oleg
 * Date: 22.12.2010
 */
public class AuthStrategy  implements IAuthorizationStrategy,
        IUnauthorizedComponentInstantiationListener {

    @Override
    public <T extends Component> boolean isInstantiationAuthorized(Class<T> componentClass) {
        if(BasePage.class.isAssignableFrom(componentClass)) {
            return PMSession.get().isSignedIn();
        }
        return true;
    }

    @Override
    public boolean isActionAuthorized(Component component, Action action) {
        return true;
    }

    @Override
    public void onUnauthorizedInstantiation(Component component) {
        throw new RestartResponseAtInterceptPageException(LoginPage.class);
    }
}
